package com.learn.springbootnetty.tcpdemo.simple;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * @author xiaodonghong
 * @description TODO
 * @date 2023/6/13-13:27
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(10087);
        InetAddress inetAddress = serverSocket.getInetAddress();
        System.out.println(inetAddress);
        int localPort = serverSocket.getLocalPort();
        System.out.println(localPort);
        SocketAddress localSocketAddress = serverSocket.getLocalSocketAddress();
        System.out.println(localSocketAddress);
        int receiveBufferSize = serverSocket.getReceiveBufferSize();
        System.out.println(receiveBufferSize);
        int soTimeout = serverSocket.getSoTimeout();
        System.out.println(soTimeout);
        // 接收客户端数据
        // 2.监听客户端连接:去死等客户端来连
        // 一旦有客户端来连，它就会返回客户端的连接对象
       while (true) {
           Socket socket = serverSocket.accept();

           // 3.从连接通道中获取输入流读取数据
      /*InputStream is = socket.getInputStream();
        // 字符输入转换流:把is变成了字符流
        InputStreamReader isr = new InputStreamReader(is);
        // 字符缓冲输入流
        BufferedReader br = new BufferedReader(isr);*/

           BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

           String line;
           while ((line = br.readLine()) != null) {
               System.out.println(line);
           }

           // int b;
           //  while((b = br.read()) != -1){
           //      System.out.print((char)b);
           //  }

           // 4.释放资源
           // 这行代码是相当于断开了跟客户端之间的连接
          // socket.close();
       }
        // 这行代码相当于就是关闭了服务器

    }
}
